package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Emp;
import com.example.demo.entity.R;
import com.example.demo.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CDHong
 * @since 2024-10-17 08:59
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private IEmpService empService;

    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,  // 页码
                  @RequestParam(defaultValue = "6") Integer limit,   // 每页显示的条数
                  String empName, Integer deptNo){

        Page<Emp> pageEmp = empService.lambdaQuery()
                .like(StringUtils.isNotEmpty(empName),Emp::getEname,empName)   // ename like '%empName%'
                .eq(Objects.nonNull(deptNo),Emp::getDeptno,deptNo)   // deptno = deptNo
                .orderByDesc(Emp::getEmpno)
                .page(new Page<>(page, limit));

        long total = pageEmp.getTotal();  // 从分页中获取总条数
        List<Emp> list = pageEmp.getRecords();  // 获取每页显示的条数

        return R.page(total,list);
    }


       // 上级
    @GetMapping("/list")
    public R list(){
        return R.data(empService.list());
    }

    @PostMapping("/save-edit")
    public R saveEdit(@RequestBody Emp emp){
        emp.setHiredate(LocalDateTime.now());
        boolean flg = empService.saveOrUpdate(emp);
        if(flg){
            return R.ok("操作成功！！");
        }
        return R.error("操作失败！！！");
    }

    @PostMapping("/del")
    public R delById(@RequestBody Emp emp){
        boolean flg = empService.removeById(emp.getEmpno());
        if(flg){
            return R.ok("删除成功！！");
        }
        return R.error("删除失败！！！");
    }










}
